package com.yunmai.cloud.web.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunmai.cloud.web.domain.CommandInfoEntity;
import com.yunmai.cloud.web.dto.commandinfo.request.ChangeParamsRequestDto;
import com.yunmai.cloud.web.services.ICommandInfoService;
import com.yunmai.core.spring.model.GridResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  实时数据查询Controller
 * </p>
 *
 * @author 赵刚
 * @since 2023-01-11
 **/
@RestController
@Validated
@RequestMapping("/v1")
public class CommandInfoController {

    @Autowired
    private ICommandInfoService commandInfoService;


    static SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @ApiOperation(
            value = "下发命令",
            notes = "下发命令",
            produces = "application/json",
            consumes = "application/json",
            response = GridResult.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "cmd", value = "命令文本", required = true, dataType = "String", example = "start/stop")
    })
    @PutMapping(value = "/commands/{cmd}")
    public ResponseEntity<?> createCommand(
            @NotEmpty(message = "命令文件不能为空")
            @PathVariable(name = "cmd",required = true)
                String cmd) {

        try {
            if (!cmd.trim().toLowerCase().equals("start") && !cmd.trim().toLowerCase().equals("stop")) {
                JSONObject errorResponse = new JSONObject();
                errorResponse.put("message", "不支持的命令。");

                ResponseEntity<JSONObject> responseError = new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
                return responseError;
            }

            JSONObject cmdJSON = new JSONObject();
            cmdJSON.put("cmd", cmd.toLowerCase().trim());

            CommandInfoEntity commandInfoEntity = new CommandInfoEntity();
            commandInfoEntity.setId(UUID.randomUUID().toString());
            commandInfoEntity.setCommandTime(new Date());
            commandInfoEntity.setCommandValue(cmdJSON.toString());
            commandInfoEntity.setIsFinished(false);
            commandInfoEntity.setCommandType(cmd.toLowerCase().trim());
            commandInfoService.create(commandInfoEntity);
            return ResponseEntity.ok().build();
        }
        catch (Exception ex)
        {
            JSONObject errorResponse = new JSONObject();
            errorResponse.put("message",ex.getMessage());

            ResponseEntity<JSONObject> responseError  = new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
            return responseError;
        }
    }

    @ApiOperation(
            value = "更新命令状态",
            notes = "更新命令状态",
            produces = "application/json",
            consumes = "application/json",
            response = GridResult.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "changeParamsRequestDto", value = "修改参数Dto", required = true, dataType = "ChangeParamsRequestDto")
    })
    @PutMapping(value = "/commands/params")
    public ResponseEntity<?> updateParams(
            @RequestBody
            ChangeParamsRequestDto changeParamsRequestDto) {
        try {
            CommandInfoEntity commandInfoEntity = new CommandInfoEntity();
            commandInfoEntity.setId(UUID.randomUUID().toString());
            commandInfoEntity.setCommandTime(new Date());
            commandInfoEntity.setCommandValue(JSONObject.toJSONString(changeParamsRequestDto));
            commandInfoEntity.setIsFinished(false);
            commandInfoEntity.setCommandType("change_params");
            commandInfoService.create(commandInfoEntity);

            return ResponseEntity.ok().build();
        }
        catch (Exception ex)
        {
            JSONObject errorResponse = new JSONObject();
            errorResponse.put("message",ex.getMessage());

            ResponseEntity<JSONObject> responseError  = new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
            return responseError;
        }
    }
}